<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/7/14
 * Time: 11:04 AM
 */

namespace Admin\Model;
use stdClass;
use Admin\Model\AdminUser;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Authentication\Result as AuthenticationResult;


class AdminUserContactTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll()
    {

    }

    public function getUserContact($id)
    {
        $resultSet = $this->tableGateway->select(array('user_id'=>$id));
        return $resultSet;
    }

    public function deleteUserContact($user_id)
    {

        $this->tableGateway->delete(array('user_id' => $user_id));
    }



    public function saveUserContact(AdminUserContact $userContact)
    {
        $data = array(
            'user_id' => $userContact->user_id,
            'contact_type'  => $userContact->contact_type,
            'contact_value'  => $userContact->contact_value,

        );

        $id = (int)$userContact->id;
        if ($id == 0) {
            if ($this->tableGateway->insert($data)) return true;
            else return false;
        } else {
            if ($this->getUser($id)) {
                if ($this->tableGateway->update($data, array('id' => $id))) return true;
                else return false;
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }



}